﻿using QD.LopPlat.UI.Code;
using QD.SQLHelper;
using QD.Tools;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace QD.LopPlat.UI
{
    public partial class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        /// <summary>
        /// 显示历史记录的Grid
        /// </summary>
        protected DataGridView Grid_History { get; set; }

        /// <summary>
        /// 历史记录的类型 0表单 1数据源 2程序 3图表 4回写 5逻辑 6功能 7接口 8关联 9通知 10计划任务 11门户 12字典 13文档 14多报表
        /// </summary>
        protected string HistoryType { get; set; }

        private ToolStripButton _ToolsBtnAdd;
        /// <summary>
        /// 注册新增菜单
        /// </summary>
        public ToolStripButton ToolsBtnAdd
        {
            get { return _ToolsBtnAdd; }
            set
            {
                _ToolsBtnAdd = value;
                _ToolsBtnAdd.Click += ToolsBtnAdd_Click;
            }
        }

        private ToolStripButton _ToolsBtnModify;
        /// <summary>
        /// 注册修改菜单
        /// </summary>
        public ToolStripButton ToolsBtnModify
        {
            get { return _ToolsBtnModify; }
            set
            {
                _ToolsBtnModify = value;
                _ToolsBtnModify.Text += "(E)";
                _ToolsBtnModify.Click += ToolsBtnModify_Click;
            }
        }

        private ToolStripButton _ToolsBtnUpdate;
        /// <summary>
        /// 注册保存菜单
        /// </summary>
        public ToolStripButton ToolsBtnUpdate
        {
            get { return _ToolsBtnUpdate; }
            set
            {
                _ToolsBtnUpdate = value;
                _ToolsBtnUpdate.Text += "(S)";
                _ToolsBtnUpdate.Click += ToolsBtnUpdate_Click; ;
            }
        }

        private ToolStripButton _ToolsBtnDelete;
        /// <summary>
        /// 注册删除菜单
        /// </summary>
        public ToolStripButton ToolsBtnDelete
        {
            get { return _ToolsBtnDelete; }
            set
            {
                _ToolsBtnDelete = value;
                _ToolsBtnDelete.Text += "(D)";
                _ToolsBtnDelete.Click += ToolsBtnDelete_Click; ;
            }
        }

        private ToolStripButton _ToolsBtnDetail;
        /// <summary>
        /// 注册详情菜单
        /// </summary>
        public ToolStripButton ToolsBtnDetail
        {
            get { return _ToolsBtnDetail; }
            set
            {
                _ToolsBtnDetail = value;
                _ToolsBtnDetail.Text += "(R)";
                _ToolsBtnDetail.Click += ToolsBtnDetail_Click; ;
            }
        }

        private ToolStripButton _ToolsBtnClose;
        /// <summary>
        /// 注册关闭菜单
        /// </summary>
        public ToolStripButton ToolsBtnClose
        {
            get { return _ToolsBtnClose; }
            set
            {
                _ToolsBtnClose = value;
                _ToolsBtnClose.Text += "(Esc)";
                _ToolsBtnClose.Click += ToolsBtnClose_Click; ;
            }
        }

        private ToolStripButton _ToolsBtnPrevious;
        /// <summary>
        /// 上一条
        /// </summary>
        public ToolStripButton ToolsBtnPrevious
        {
            get { return _ToolsBtnPrevious; }
            set
            {
                _ToolsBtnPrevious = value;
                _ToolsBtnPrevious.Text += "(Up)";
                _ToolsBtnPrevious.Click += ToolsBtnPrevious_Click;
            }
        }

        private ToolStripButton _ToolsBtnNext;
        /// <summary>
        /// 上一条
        /// </summary>
        public ToolStripButton ToolsBtnNext
        {
            get { return _ToolsBtnNext; }
            set
            {
                _ToolsBtnNext = value;
                _ToolsBtnNext.Text += "(Down)";
                _ToolsBtnNext.Click += ToolsBtnNext_Click;
            }
        }

        protected virtual void ToolsBtnNext_Click(object sender, EventArgs e)
        {
        }

        protected virtual void ToolsBtnPrevious_Click(object sender, EventArgs e)
        {
        }

        protected virtual void ToolsBtnClose_Click(object sender, EventArgs e)
        {
            if (PromptHelper.PromptOkCancel("您确认关闭当前窗口？") == DialogResult.OK)
            {
                this.Close();
            }
        }

        protected virtual void ToolsBtnDetail_Click(object sender, EventArgs e)
        {
        }

        protected virtual void ToolsBtnDelete_Click(object sender, EventArgs e)
        {
        }

        protected virtual void ToolsBtnUpdate_Click(object sender, EventArgs e)
        {
        }

        protected virtual void ToolsBtnModify_Click(object sender, EventArgs e)
        {
        }

        public virtual void ToolsBtnAdd_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        protected virtual void InitUIData(string flagValue)
        {
            LoadHistory();
        }

        /// <summary>
        /// 显示历史记录
        /// </summary>
        protected virtual void LoadHistory()
        {     
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="flagValues"></param>
        protected virtual void DeleteData(params string[] flagValues) { }

        private void BaseForm_Load(object sender, EventArgs e)
        {

        }

        void Grid_History_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                string dbCode = this.Grid_History.Rows[e.RowIndex].Cells["SCode"].Value.ToString();
                InitUIData(dbCode);
            }
        }

        void BaseForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.S)
            {
                if (ToolsBtnUpdate != null)
                {
                    ToolsBtnUpdate_Click(sender, e);
                }
            }
            //不为新增增加快捷键，因为有些功能不是管理员不能直接新增
            //else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            //{
            //    if (ToolsBtnAdd != null)
            //    {
            //        ToolsBtnAdd_Click(sender, e);
            //    }
            //}
            else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.E)
            {
                if (ToolsBtnModify != null)
                {
                    ToolsBtnModify_Click(sender, e);
                }
            }
            else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.D)
            {
                if (ToolsBtnDelete != null)
                {
                    ToolsBtnDelete_Click(sender, e);
                }
            }
            else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.R)
            {
                if (ToolsBtnDetail != null)
                {
                    ToolsBtnDetail_Click(sender, e);
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                if (ToolsBtnClose != null)
                {
                    ToolsBtnClose_Click(sender, e);
                }
            }
            else if (e.KeyCode == Keys.PageUp)
            {
                if (ToolsBtnPrevious != null)
                {
                    ToolsBtnPrevious_Click(sender, e);
                }
            }
            else if (e.KeyCode == Keys.PageDown)
            {
                if (ToolsBtnNext != null)
                {
                    ToolsBtnNext_Click(sender, e);
                }
            }
        }
    }
}
